<template>
  <el-form
    ref="deptForm"
    :model="form"
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    flag: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    // 部门编号非重规则
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map((item) => item.code)
      // 判断是否为编辑状态
      if (this.flag) {
        // 获取到当前部门的对象
        const curp = this.originList.find((item) => item.id === this.pid)
        // 从当前对象里面获取code
        const curCode = curp.code
        // 过滤出当前code
        existCodeList = existCodeList.filter((item) => item.code !== curCode)
      }
      // 在已有的部门编码进行判断
      if (existCodeList.includes(value)) {
        callback(new Error(value + '部门编码不能重复'))
      } else {
        callback()
      }
    }
    // 部门名称非重规则
    const validName = (rule, value, callback) => {
      // 获取到当前部门下的所有的name
      let existNameList = this.originList
        .filter((item) => item.pid === this.pid)
        .map((item) => item.name)

      // 判断是否为编辑状态
      if (this.flag) {
        console.log(this.originList)
        // 格式： 被占用的名称 = 当前的所有名称(自己和兄弟) - 当前的名称(自己)
        // 根据当前的id找到 对应 部门对象 里面的 pid
        const pid = this.originList.find((item) => item.id === this.pid)
        // 在根据找到的pid过滤掉 不行他兄弟的元素 并且也要把自己过滤掉
        existNameList = this.originList
          .filter((item) => item.pid === pid.pid && item.id !== this.pid)
          .map((item) => item.name)
        // console.log(existNameList)
      }

      // 在已有的部门名称进行判断
      if (existNameList.includes(value)) {
        callback(new Error(value + '部门名称不能重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: validName,
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: validCode,
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getEmployees()
    // this.loadDetail()
  },
  methods: {
    // 添加部门信息
    async addToDo() {
      const d = { ...this.form, pid: this.pid }
      const res = await addDepartments(d)
      this.$emit('boolean')
      this.$message.success(res.message)
    },
    // 更新部门信息
    async setToDo() {
      const res = await updateDepartments(this.form)
      this.$emit('boolean')
      this.$message.success(res.message)
    },
    // 点击确认按钮区分编辑和添加
    hSubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return
        this.flag ? this.setToDo() : this.addToDo()
      })
    },
    // 获取部门详情
    async loadDetail() {
      if (this.flag) {
        const res = await getDepartDetail(this.pid)
        this.form = res.data
      }
    },
    // 取消弹窗
    hCancel() {
      this.$emit('boolean')
    },
    // 重置方法
    restForm() {
      // 重置表单数据并清空校验结果
      this.$refs.deptForm.resetFields()
    },
    // 获取部门列表
    async getEmployees() {
      const res = await getEmployeeSimple()
      this.list = res.data
    }
  }
}
</script>

<style></style>
